package com.sharedlease.utils;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

import static com.sharedlease.utils.EmailConstant.*;

/**
 * 邮件发送 utils
 * 目前缺点: 邮件中附带的超链接指向的 url 固定了
 * @author Laymedown
 */
public class EmailUtils implements Runnable{

    /**
     * 收件人邮箱
     */
    private String email;

    private String code;

    private Integer flag;

    public EmailUtils(String email, String code){
        this.email = email;
        this.code = code;
    }

    /**
     * 注册身份验证
     * @param email
     * @param code
     * @throws Exception
     */
    public static void sendMailForVer(String email, String code) throws Exception{
        EmailUtils emailUtils = new EmailUtils(email, code);
        emailUtils.flag = 0;
        emailUtils.run();
    }

    /**
     * 忘记密码
     * @param email
     * @param password
     */
    public static void sendEmailForPsd(String email, String password){
        EmailUtils emailUtils = new EmailUtils(email, password);
        emailUtils.flag = 1;
        emailUtils.run();
    }


    @Override
    public void run() {
        Properties properties = new Properties();
        // 邮件服务器
        properties.setProperty("mail.smtp.host","smtp.qq.com");
        // 选定协议
        properties.setProperty("mail.transport.protocol","smtp");
        // 身份认证
        properties.setProperty("mail.smtp.auth","true");

        try{
            // 针对QQ邮箱， 需要特性设置SSL加密
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);

            // 获取发送 session
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    // 发件人邮箱账号、授权码
                    return new PasswordAuthentication(sender_mail, authorization_code);
                }
            });

            // 创建邮件对象
            Message message = new MimeMessage(session);
            // 设置发件人
            message.setFrom(new InternetAddress(sender_mail));
            // 设置接收人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

            String content = "";
            if (flag == 0) {
                // 设置邮件主题
                message.setSubject(subject_verification);
                // 设置邮件内容
                content = "<h1>您的注册验证码是："+code+"</h1>";
            }else {
                message.setSubject(subject_forget_password);
                // 设置邮件内容
                content = "<h1>您的密码是："+code+"</h1>";
            }
            message.setContent(content, "text/html;charset=UTF-8");
            Transport.send(message);
            System.out.println("邮件成功发送!");
        } catch (Exception e) {
            System.out.println("邮件发送失败");
            e.printStackTrace();
        }
    }

}
